package com.action;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.dao.ShopInfo;
import com.dbserver.Condb;
import com.server.InfoService;
import com.server.ShopListService;

public class ShopList {
	 //3.商家列表
	 public String GetShopList(){ 
		  HttpServletRequest request = ServletActionContext.getRequest ();
		  //获取类型
		  int type=-1;
		  if(request.getParameter("type")!=null)
		  {
			  type=Integer.valueOf(request.getParameter("type"));
		  }	
		  
		  //用户排序
		  int orderByDateOrOrder=1;
		  if(request.getParameter("orderByDateOrOrder")!=null)
		  {
			  orderByDateOrOrder=Integer.valueOf(request.getParameter("orderByDateOrOrder"));
		  }
		  String pageNumberStr = request.getParameter("pageNumber");
		  int pageNumber = 1;  
		  if(pageNumberStr!=null && !pageNumberStr.isEmpty())  
		  {  
		        pageNumber = Integer.parseInt(pageNumberStr);  
		  }  
		    
		  int pageSize = 10; //分页大小  
		  ShopListService shopListService =new ShopListService();
		  ArrayList<ShopInfo> shopList=shopListService.getShop(type, orderByDateOrOrder, pageNumber, pageSize);
		  int totalPosts=0; //问题总数  
	      //处理结果集
		  Condb condb=new Condb();
		  String sql="select count(id) from shopinfo";
	   	  if(type!=-1)
	      {
	   		sql+=" where type="+type;
	   	  }
		  ResultSet rs=condb.query(sql);
		  try {
				if(rs.next())
				{
					totalPosts=rs.getInt(1);
				}
				rs.close();
				condb.close();
		  } catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
	      }
		    
		  int totalPages = totalPosts/pageSize + ((totalPosts%pageSize)>0?1:0); //计算得出的总页数  
		  request.setAttribute("pageSize", pageSize);  
	      request.setAttribute("totalPosts", totalPosts);  
	      request.setAttribute("pageNumber", pageNumber);  
	      request.setAttribute("totalPages", totalPages);  
	      request.setAttribute("entryList", shopList);
	       //下拉列表的值
		   InfoService serviceTypeService=new InfoService();
		   Map typeM=serviceTypeService.getType();
		   request.setAttribute("typeS", typeM);
	      return "success"; 
	    }  
}
